const state = {
  fatherPage: {},
  fatherList: [],
  typePage: {},
  typeList: [],
  list: [],
  page: {},
}

const actions = {
  saveTypes: ({ commit }, payload) => {
    commit('SAVETYPES', payload)
  },
  saveList: ({ commit }, payload) => {
    commit('SAVELIST', payload)
  },
  saveFatherTypes: ({ commit }, payload) => {
    commit('SAVEFATHERTYPES', payload)
  }
}

const mutations = {
  SAVETYPES: (state, payload) => {
    state.typeList = payload.result
    state.typePage = {
      number: payload.number,
      total: payload.resultNum,
      size: 10
    }
  },
  SAVEFATHERTYPES: (state, payload) => {
    state.fatherList = payload.result
    state.fatherPage = {
      number: payload.number,
      total: payload.resultNum,
      size: 10
    }
  },
  SAVELIST: (state, payload) => {
    if (payload.result.length === 0) {
      state.list = []
    } else {
      state.list = payload.result.map(item => {
        return {
          id: item.id,
          title: item.title,
          subTitle: item.sub_title,
          imgUrl: item.img_url,
          name: item.name,
          period: item.period,
          summary: item.summary,
          content: item.content,
          typeId: item.type_id,
          year: item.s_year,
          order: item.show_order,
          state: item.state,
          clickTimes: item.click_times
        }
      })
    }
    state.page = {
      number: payload.number,
      total: payload.resultNum,
      size: 10
    }
  },
}

export default {
  state,
  actions,
  mutations
}